package com.qiguliuxing.dts.merchant.service;

import com.github.pagehelper.PageHelper;
import com.qiguliuxing.dts.common.type.AdminTypeEnum;
import com.qiguliuxing.dts.common.type.RoleTypeEnum;
import com.qiguliuxing.dts.common.util.bcrypt.BCryptPasswordEncoder;
import com.qiguliuxing.dts.db.bean.DtsMerchantApplyInfo;
import com.qiguliuxing.dts.db.domain.DtsAdmin;
import com.qiguliuxing.dts.db.domain.DtsAdminExample;
import com.qiguliuxing.dts.service.base.DtsAdminService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商户端管理用户服务类
 * @author qiguliuxing
 * @since 1.0.0
 */
@Service
public class MerchantAdminService extends DtsAdminService {

    public List<DtsAdmin> querySelective(Integer brandId, String username, Integer page, Integer limit, String sort, String order) {
        DtsAdminExample example = new DtsAdminExample();
        DtsAdminExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        criteria.andDeletedEqualTo(false);
        // 过滤商户数据
        if (brandId != null) {
            criteria.andBrandIdEqualTo(brandId);
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);
        return adminMapper.selectByExampleSelective(example, result);
    }

    /**
     * 查询商户管理员
     * @param username
     * @return
     */
    public List<DtsAdmin> findMerchantAdmin(String username) {
        DtsAdminExample example = new DtsAdminExample();
        example.or().andUsernameEqualTo(username).andTypeEqualTo(AdminTypeEnum.MERCHANT.getType()).andDeletedEqualTo(false);
        return adminMapper.selectByExample(example);
    }

    /**
     * 查询商户管理员（全部：不论删除否）
     * @param username
     * @return
     */
    public DtsAdmin findAllMerchantAdmin(String username) {
        DtsAdminExample example = new DtsAdminExample();
        example.or().andUsernameEqualTo(username).andTypeEqualTo(AdminTypeEnum.MERCHANT.getType());
        return adminMapper.selectOneByExample(example);
    }

    public DtsAdmin findMerchantAdminByEmail(String email) {
        DtsAdminExample example = new DtsAdminExample();
        example.or().andMailEqualTo(email).andTypeEqualTo(AdminTypeEnum.MERCHANT.getType());
        return adminMapper.selectOneByExample(example);
    }

    public void saveAdminUserInfo(DtsMerchantApplyInfo merchantApplyInfo) {
        DtsAdmin admin = new DtsAdmin();
        setAdminInfo(merchantApplyInfo, admin);
        this.add(admin);
    }

    public void updateAdminUserInfo(DtsMerchantApplyInfo merchantApplyInfo,DtsAdmin admin) {
        this.setAdminInfo(merchantApplyInfo, admin);
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    private void setAdminInfo(DtsMerchantApplyInfo merchantApplyInfo, DtsAdmin admin) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        admin.setUsername(StringUtils.trim(merchantApplyInfo.getUsername()));
        admin.setPassword(encoder.encode(merchantApplyInfo.getPass()));
        admin.setAvatar(merchantApplyInfo.getAvatar());
        admin.setMail(StringUtils.trim(merchantApplyInfo.getEmail()));
        admin.setDeleted(Boolean.TRUE);//商户入驻刚申请此字段设置逻辑删除（无法登录）
        admin.setTel(StringUtils.trim(merchantApplyInfo.getPhone()));
        admin.setType(AdminTypeEnum.MERCHANT.getType());
        admin.setRoleIds(new Integer[]{Integer.valueOf(RoleTypeEnum.MERCHANT.getType())});
    }
}
